package love.bot.forli.group.review.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import love.bot.forli.group.review.bean.Dto.QuestionAllDto;
import love.bot.forli.group.review.bean.Option;
import love.bot.forli.group.review.bean.Result;
import love.bot.forli.group.review.bean.Vo.AddQuestionVo;
import love.bot.forli.group.review.bean.Vo.EditQuestionVo;
import love.bot.forli.group.review.entity.Question;
import love.bot.forli.group.review.entity.QuestionOption;
import love.bot.forli.group.review.mapper.QuestionGroupMapper;
import love.bot.forli.group.review.mapper.QuestionMapper;
import love.bot.forli.group.review.mapper.QuestionOptionMapper;
import love.bot.forli.group.review.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static love.bot.forli.group.review.util.Constants.CHECK_QUESTION;
import static love.bot.forli.group.review.util.Constants.SIMPLE_QUESTION;

/**
 * @author Kontori
 */
@Service
@Slf4j
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    private QuestionGroupMapper questionGroupMapper;

    @Autowired
    public void setQuestionGroupMapper(QuestionGroupMapper questionGroupMapper) {
        this.questionGroupMapper = questionGroupMapper;
    }

    private QuestionMapper questionMapper;

    @Autowired
    public void setQuestionMapper(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    private QuestionOptionMapper questionOptionMapper;

    @Autowired
    public void setQuestionOptionMapper(QuestionOptionMapper questionOptionMapper) {
        this.questionOptionMapper = questionOptionMapper;
    }

    @Override
    public Result<Object> addQuestion(AddQuestionVo addQuestionVo) {
        if (questionGroupMapper.selectById(addQuestionVo.getQuestionGroupId()) == null) {
            return Result.fail("对应题组不存在");
        }
        //正确答案列表转化
        String answer = convertAnswer(addQuestionVo.getAnswers(), addQuestionVo.getType());
        if ("".equals(answer)) {
            return Result.fail("请检查正确答案");
        }
        //插入题目
        Question question = new Question();
        BeanUtils.copyProperties(addQuestionVo, question);
        question.setAnswer(answer);
        questionMapper.insert(question);
        //插入选项
        insertOption(question, addQuestionVo.getOptions());
        return Result.success("新增成功", null);
    }

    @Override
    public Result<Object> deleteQuestion(Integer questionId) {
        Question question = questionMapper.selectById(questionId);
        if (question == null) {
            return Result.fail("对应题目不存在");
        }
        //删除题目选项
        questionOptionMapper.delete(new QueryWrapper<QuestionOption>().eq("question_id", questionId));
        //删除题目
        questionMapper.deleteById(question);
        return Result.success("删除成功", null);
    }

    @Override
    public QuestionAllDto getQuestionInfoById(Integer questionId) {
        QuestionAllDto questionAllDto = new QuestionAllDto();
        Question question = questionMapper.selectById(questionId);
        BeanUtils.copyProperties(question, questionAllDto);
        questionAllDto.setOptions(questionOptionMapper.selectList(new QueryWrapper<QuestionOption>()
                .eq("question_id", questionId)));
        return questionAllDto;
    }

    @Override
    public List<QuestionAllDto> getQuestionInfosByGroupId(Integer questionGroupId) {
        List<QuestionAllDto> questionAllDtoList = new ArrayList<>();
        questionMapper.selectList(new QueryWrapper<Question>()
                        .eq("question_group_id", questionGroupId))
                .forEach(question -> questionAllDtoList.add(getQuestionInfoById(question.getQuestionId())));
        return questionAllDtoList;
    }

    @Override
    public Result<Object> editQuestion(EditQuestionVo editQuestionVo) {
        try {
            //删除选项
            questionOptionMapper.delete(new QueryWrapper<QuestionOption>()
                    .eq("question_id", editQuestionVo.getQuestionId()));
            //正确答案列表转化
            String answer = convertAnswer(editQuestionVo.getAnswers(), editQuestionVo.getType());
            if ("".equals(answer)) {
                return Result.fail("请检查正确答案");
            }
            //更新题目
            Question question = new Question();
            question.setAnswer(answer);
            BeanUtils.copyProperties(editQuestionVo, question);
            questionMapper.updateById(question);
            //插入选项
            insertOption(question, editQuestionVo.getOptions());
            return Result.success("修改成功", null);
        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }

    private void insertOption(Question question, List<Option> options) {
        if (options != null) {
            options.forEach(option -> {
                QuestionOption questionOption = new QuestionOption();
                questionOption.setQuestionId(question.getQuestionId());
                questionOption.setContent(option.getContent());
                questionOption.setOrder(option.getOrder());
                questionOptionMapper.insert(questionOption);
            });
        }
    }

    private String convertAnswer(List<Boolean> ans, Integer type) {
        if (type == CHECK_QUESTION) {
            return ans.get(0) ? "1" : "0";
        } else {
            List<String> answers = new ArrayList<>();
            for (int i = 0; i < ans.size(); i++) {
                if (ans.get(i)) {
                    answers.add(String.valueOf(i));
                }
            }
            if (answers.size() == 0) {
                return "";
            }
            if (type == SIMPLE_QUESTION && answers.size() > 1) {
                return "";
            }
            return String.join(",", answers);
        }
    }
}
